package com.ingsing.tourism.controller;

import com.ingsing.common.enmus.ResultCode;
import com.ingsing.common.resp.Base;
import com.ingsing.common.resp.Resp;
import com.ingsing.tourism.entity.User;
import com.ingsing.tourism.entity.UserInfo;
import com.ingsing.tourism.service.Impl.UserInfoServiceImpl;
import com.ingsing.tourism.service.Impl.UserServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * @author: NALHOUG
 * @time: 2019/10/15 19:37
 * @explain 用户
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {
    @Resource
    private UserServiceImpl userService;
    @Resource
    private UserInfoServiceImpl userInfoService;

    //用户登出
    @ResponseBody
    @RequestMapping(value = "/logout",method = RequestMethod.POST)
    public Resp logout(HttpServletRequest request){
        Resp resp = new Resp();
        System.out.println("当前用户Session："+request.getSession().getAttribute(Base.THIS_USER));
        request.getSession().removeAttribute(Base.THIS_USER);
        resp.setResultCode(ResultCode.LOGOUT_SUCCESS);
        System.out.println("用户注销后："+request.getSession().getAttribute(Base.THIS_USER+request.getSession().getId()));
        return resp;
    }

    //查看资料
    @ResponseBody
    @RequestMapping(value = "/info",method = RequestMethod.POST)
    public Resp checkInfo(HttpServletRequest request){
        Resp resp = new Resp();
        User sUser = thisAccount(request);
        System.out.println("当前Session用户:"+sUser);
        System.out.println((sUser).getUserId());
        //查询当前用户的详情信息
        sUser.setPassword(null);
        resp.setData(sUser);
        resp.setResultCode(ResultCode.SUCCESS);
        System.out.println(resp);
        return resp;
    }
    //查看更多资料
    @ResponseBody
    @RequestMapping(value = "/more",method = RequestMethod.POST)
    public Resp checkMore(HttpServletRequest request){
        Resp resp = new Resp();
        User sUser = thisAccount(request);
        UserInfo userInfo = userInfoService.checkUserInfo(sUser.getUserId());
        System.out.println(userInfo);
        resp.setData(userInfo);
        return resp;
    }


    //用户修改资料
    @ResponseBody
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Resp check(User user,HttpServletRequest request){
        Resp resp = new Resp();
        return resp;
    }

    //关闭账户
    @ResponseBody
    @RequestMapping(value = "/offUser",method = RequestMethod.POST)
    public Resp offUser(User user,HttpServletRequest request){
        Resp resp = new Resp();
        return resp;
    }

    private User thisAccount(HttpServletRequest request){
        //cong
        User sUser = (User)request.getSession().getAttribute(Base.THIS_USER);
        return sUser;
    }




}
